

function Input( _parent, _object )
{
    if ( typeof( _object ) == "undefined" )
        _object = new Object();
    
    this.parent = _parent;
    if ( typeof( _object.name) )
        this.name = _object._name;

    this.layoutBuilt = false;

    if ( this.parent.appendChild )
    {
        // a parent DOM elem
        this.layout( this.parent );
    }

    if ( typeof( _object.callbackBlur ) )
        this.callbackBlur = _object.callbackBlur;
    
    if ( typeof( _object.callbackSave ) )
        this.callbackSave = _object.callbackSave;    
}

Input.prototype.buildLayout = function()
{
    this.domElement = document.createElement( 'span' );
    this.domElement.style.position = "relative";

    this.inputElement = document.createElement( 'input' );
    this.inputElement.setAttribute( 'name', this.name );
    this.inputElement.style.position = "relative";
    this.inputElement.setAttribute( 'autocomplete', 'off' );
    this.inputElement.title = "Press ENTER to save your changes";

    var myself = this;

    this.domElement.appendChild( this.inputElement );
    this.layoutBuilt = true;
    
    this.binds();
}

Input.prototype.layout = function( node )
{
    if ( !this.layoutBuilt )
    {
        this.buildLayout();
    }
    
    if( node.appendChild )
    {
        node.appendChild( this.domElement );
    }
}

Input.prototype.getValue = function()
{
    return this.inputElement.value;
}

Input.prototype.setValue = function( _value )
{
    if ( typeof( _value ) == "object" )
    {
        _value = _value.value;
    }
    this.inputElement.value = _value;
}

Input.prototype.remove = function()
{
    try
    {
        this.domElement.parentNode.removeChild( this.domElement.parentNode );
    }
    catch( er )
    {
    }
}

Input.prototype.focus = function()
{
    this.inputElement.focus();
}

Input.prototype.binds = function()
{
    var copy = this;
    if ( this.inputElement.addEventListener )
    {
        this.inputElement.addEventListener( 'blur', function( _event ){ copy.EventListeners.handleBlur.call( copy, _event ); }, false );
        this.inputElement.addEventListener( 'keydown', function( _event ){ copy.EventListeners.handleKeyUp.call( copy, _event ); }, false );
    }
    else
    {
        this.inputElement.attachEvent( 'onblur', function( _event ){ copy.EventListeners.handleBlur.call( copy, _event ); }, false );
        this.inputElement.attachEvent( 'onkeydown', function( _event ){ copy.EventListeners.handleKeyUp.call( copy, _event ); }, false );                
    }
}

Input.prototype.EventListeners =
{
    handleBlur : function( _event )
    {
        if ( this.callbackBlur )
            this.callbackBlur.call( this, _event );
    },
    handleKeyUp : function( _event )
    {
        if ( _event.keyCode == 13 )
        {
            if ( this.callbackSave )
                this.callbackSave.call( this, _event );
            
            if ( _event.stopPropagation )
            {
                _event.stopPropagation();
            }
            else if ( window.event )
            {
                window.event.cancelBubble = true;
            }
        }
        
        if( _event.keyCode == 27 )
        {
            this.inputElement.blur();
        }
    }    
}
